package com.proposition.commons.exam.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.proposition.commons.exam.exception.ExamImportException;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.model.ExamCognitiveLevel;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 14:12 2017/12/1
 * @description:
 */
public class Util {
    /**
     * @Author: 陈玉林
     * @Date: 2017/8/23 9:23
     * @Description: 验证试题题号
     * 不小于1，不得大于int最大值
     */
    public static ExamImportException isLegalExamNum(String content) {
        //保证为数字
        if (content == null || content.length()==0) {
            return new ExamImportException(ResultEnum.ILLEGAL_EXAM_NUM);
        }
        if (!content.matches("^\\d+$")) {
            return new ExamImportException(content, ResultEnum.ILLEGAL_EXAM_NUM);
        }
        //保证不能超过int最大范围，不小于1
        if (new BigInteger(content).compareTo(new BigInteger("1")) >= 0 && new BigInteger(content).compareTo(new BigInteger("2147483647")) <= 0) {
            Integer examNum = Integer.valueOf(content);
            //buneng大于250
            if (examNum > 250) {
                return new ExamImportException(content, ResultEnum.ILLEGAL_EXAM_NUM);
            }
        } else {
            return new ExamImportException(content, ResultEnum.ILLEGAL_EXAM_NUM);
        }
        return null;
    }

    /**
     * @Author: 陈玉林
     * @Date: 2017/8/23 10:54
     * @Description: 字段不可为空
     */
    public static void isEmpty(String getFieldValueFromTables, String examNum) {
        if (getFieldValueFromTables == null || getFieldValueFromTables == "") {
            throw new ExamImportException(examNum, ResultEnum.ILLEGAL_FIELD);
        }
    }

    /**
     * @Author: 陈玉林
     * @Date: 2017/8/21 13:18
     * @Description: 判断认知层次是否正确
     */
    public static ExamImportException isLegalCognitiveLevel(String content, String examNum) {
        if (content == null || content.length()==0) {
            return new ExamImportException(content, ResultEnum.EMPTY_COGNITIVE_LEVEL);
        } else {
            ExamCognitiveLevel cognitiveLevel = new ExamCognitiveLevel().selectOne(new EntityWrapper().eq("name", content));
            if (cognitiveLevel==null) {
                return new ExamImportException(examNum, ResultEnum.ILLEGAL_COGNITIVE_LEVEL);
            }
        }
        return null;
    }

    /**
     * @Author: 陈玉林
     * @Date: 2017/8/23 11:30
     * @Description: 判断带有分数的答案格式
     */
    public static ExamImportException isLegalAnswerWithScore(String label,String field, String examNum, Float maxScore) {
        //非负浮点数正则
        String regFloat = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
        //整数正则
        String regInt = "^\\d+$";
        if (field == null ) {
            return new ExamImportException(label,examNum, ResultEnum.ILLEGAL_FIELD);
        } else if (field.length()==0) {
            return new ExamImportException(label,examNum, ResultEnum.ILLEGAL_EXAM_FIELD);
        } else {
            if (!field.contains("-")) {
                return new ExamImportException(label, examNum, ResultEnum.ILLEGAL_ANSWER_SCORE);
            } else {
                int last = field.lastIndexOf("-");
                String answer = field.substring(0, last).trim();
                String score = field.substring(last + 1).trim();
                System.out.println(score.hashCode());
                if (score == null || String.valueOf(score).equals("") || answer == null || answer == "") {
                    return new ExamImportException(label, examNum, ResultEnum.ILLEGAL_ANSWER_SCORE);
                } else {
                    if (!score.matches(regFloat) && !score.matches(regInt) || new BigDecimal(score).compareTo(new BigDecimal("0")) <= 0 || new BigDecimal(score).compareTo(new BigDecimal(maxScore)) > 0) {
                        Result result = new Result();
                        result.setCode(ResultEnum.ILLEGAL_ANSWER_SCORE.getCode());
                        result.setMsg("<span style='font-weight: bold;color: red'>第" + examNum + "题:</span>" + label + "，答案分数格式不正确，请填写大于0小于" + maxScore + "的数字！");
                        return new ExamImportException(result);
                    }
                }
            }
        }
        return null;
    }


    /**
     * @Author: 陈玉林
     * @Date: 2017/8/23 14:20
     * @Description: 获取答案或者分数
     * 0：答案
     * 1：分数
     */
    public static String getAnswerOrScore(Integer target, String field) {
        int last = field.lastIndexOf("-");
        switch (target) {
            case 0:
                return field.substring(0, last);
            case 1:
                return field.substring(last + 1);
            default:
                return null;
        }
    }

    /**
     *@author: 陈玉林 
     *@date: 2018/4/16 9:40 
     *@description: 验证试题模板是否被修改
     */
    public static boolean validateTemplateIsChange(Map<String, String> examMap, String field) {
        boolean flag = false;
        if (examMap!=null) {
            if (!examMap.containsKey(field)) {
                flag = true;
            }
        }
        return flag;
    }
}
